﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.OrmLite;
using ServiceStack;
using EquipmentManager.Dtos;

namespace EquipmentManager.Services
{
    class TodoService: Service
    {

        public GetTodosResponse Get(GetTodos request)
        {
            Session sessionInfo = RequestLoginValidateTools.CheckLogin(this.Request);
            List<Todo> ress;
            if (request.Id != 0)
            {
                ress = new List<Todo>();
                Todo tmp = Db.SingleById<Todo>(request.Id);
                if(tmp != null && tmp.UserName == sessionInfo.UserName)
                    ress.Add(tmp);
            }
            else
            {
                ress = Db.Select<Todo>(request.Filter).Where(q=>q.UserName == sessionInfo.UserName).ToList();
            }

            return new GetTodosResponse { Entities = ress };
        }
        public PostTodoResponse Post(PostTodo request)
        {
            Session sessionInfo = RequestLoginValidateTools.CheckLogin(this.Request);
            request.Entity.UserName = sessionInfo.UserName;
            Db.Save<Todo>(request.Entity);
            return new PostTodoResponse { Id = request.Entity.Id };
        }
        public DeleteTodoResponse Delete(DeleteTodo request)
        {
            Session sessionInfo = RequestLoginValidateTools.CheckLogin(this.Request);
            Db.DeleteById<Todo>(request.Id);
            return new DeleteTodoResponse();
        }
    }
}
